package io.gitee.chearnee.fw.datasecurity;

import io.gitee.chearnee.fw.datasecurity.advice.SDBodyAdvice;
import io.gitee.chearnee.fw.datasecurity.core.executor.desensitization.SDExecutorFactory;
import io.gitee.chearnee.fw.datasecurity.core.handler.desensitization.SDRuleHandlerFactory;
import io.gitee.chearnee.fw.datasecurity.core.properties.DataSecurityDbCryptProperty;
import io.gitee.chearnee.fw.datasecurity.core.properties.DataSecurityDesensitizationProperty;
import io.gitee.chearnee.fw.datasecurity.mybatisplus.inspect.MybatisInterceptorInspect;
import io.gitee.chearnee.fw.datasecurity.sidecar.SideCarConfig;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;


/**
 * @author cn
 * @version 1.0
 * @description
 * @date 11/11/2022 6:17 PM
 */
@Import({SDBodyAdvice.class, SideCarConfig.class})
@EnableConfigurationProperties({DataSecurityDbCryptProperty.class,
        DataSecurityDesensitizationProperty.class})
public class DataSecurityAutoConfig {
    @Bean
    public MybatisInterceptorInspect mybatisInterceptorInspect(SqlSessionFactory sessionFactory, DataSecurityDbCryptProperty dbCryptProperty) {
        return new MybatisInterceptorInspect(sessionFactory, dbCryptProperty);
    }
    @PostConstruct
    public void initFactory() {
        SDExecutorFactory.initExecutorFactory();
        SDRuleHandlerFactory.initRuleHandler();
    }

    @PreDestroy
    public void destroy() {
        SDExecutorFactory.destroy();
        SDRuleHandlerFactory.destroy();
    }

}
